package cn.mg.framework.util.date;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.FastDateFormat;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;

public class DateUtil {

    public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
    public static final String DEFAULT_DATE_FORMAT2 = "yyyy-MM-dd";
    public static final FastDateFormat FAST_DATE_FORMAT= FastDateFormat.getInstance(DEFAULT_DATE_FORMAT2);

    /**
	 * 取得中国上海当前时间
	 * 类型:[String]
	 * 格式:[yyyy-MM-dd HH:mm:ss]
	 * @return
	 */
	public static String getShangHaiCurrentDateTimeForString(){
		
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");	
		
		Calendar calendar = new GregorianCalendar();		
		
		//设置时区为上海
		TimeZone zh = TimeZone.getTimeZone("Asia/Shanghai");	
		calendar.setTimeZone(zh);
		format.setTimeZone(zh);
		
		return format.format(calendar.getTime());
	}
	
	/**
	 * 取得中国上海当前时间
	 * 类型:[Date]
	 * 格式:[yyyy-MM-dd HH:mm:ss]
	 * @return
	 */
	public static Date getShangHaiCurrentDateTimeForDate(){
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		Date formatDate = null;
		try {
			formatDate = format.parse(getShangHaiCurrentDateTimeForString());
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		return formatDate;
	}
	
    /**
     * 取得当前时间
     * @return
     */
    public static Date getCurrentDateTime(){
        Calendar calendar = new GregorianCalendar();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date currentDate = calendar.getTime();

        Date formatDate = null;
        try {
            formatDate = format.parse(format.format(currentDate));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return formatDate;
    }

    /**
     * 根据格式获取当前时间
     * @param pattern
     * @return
     */
    public static Date getCurrentDateTime(String pattern){
        Calendar calendar = new GregorianCalendar();
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        Date currentDate = calendar.getTime();

        Date formatDate = null;
        try {
            formatDate = format.parse(format.format(currentDate));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return formatDate;
    }

    /**
     * 取得格式化后的当前系统时间[yyyy-MM-dd HH:mm:ss]
     * @return
     */
    public static String getCurrentSytemTimeForString(){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(getCurrentDateTime());
    }

    /**
     * 取得格式化后的当前系统时间[Timestamp类型]
     * @return
     */
    public static Timestamp getCurrentSytemTimeForTimestamp(){

        Calendar calendar = new GregorianCalendar();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date currentDate = calendar.getTime();
        Date formatDate = null;
        try {
            formatDate = format.parse(format.format(currentDate));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return Timestamp.valueOf(format.format(formatDate));
    }

    /**
     * 取得格式化后的当前系统日期[yyyy-MM-dd]
     * @return
     */
    public static String getCurrentSytemDateForString(){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(getCurrentDateTime());
    }

    /**
     * 取得格式化后的当前系统时间[字符串类型]
     * @return
     */
    public static String getCurrentSytemTimeForStringByPackagesCode(){
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        return format.format(getCurrentDateTime());
    }

    /**
     * 按照yyyy-MM-dd格式，将 Date 对象转换成字符串
     * @param date
     * @return
     */
    public static String formatDateByDet(Date date){
        if(date==null){
            return "";
        }
        SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return formater.format(date);
    }

    public static String formatDate(Date date,String pattern){
        if(StringUtils.isBlank(pattern)){
            return formatDateByDet(date);
        }else{
            SimpleDateFormat formater = new SimpleDateFormat(pattern);
            return formater.format(date);
        }
    }
    /**
     * 按照指定格式转换日期
     * @param dateStr
     * @param pattern
     * @return
     */
    public static Date parseDate(String dateStr,String pattern){
        if(StringUtils.isBlank(dateStr)){
            return null;
        }
        SimpleDateFormat formater=new SimpleDateFormat(pattern);
        try {
            return formater.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
    
    /**
	 * 判断两个时间的大小.
	 * 
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	public static boolean isCompareTime(Date startTime, Date endTime) {
		if (endTime.getTime() > startTime.getTime()) {
			return true;
		} else {
			return false;
		}
	}
	
	/**
	 * 相差秒数
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	public static Long getMillisBetween(Date startDate, Date endDate) {
		Calendar d1 = Calendar.getInstance();
		d1.setTime(startDate);
		Calendar d2 = Calendar.getInstance();
		d2.setTime(endDate);
		Long min = 0L;
		if (d1.getTimeInMillis() > d2.getTimeInMillis()) {
			min = (d1.getTimeInMillis() - d2.getTimeInMillis())/1000;
		} else {
			min = (d2.getTimeInMillis() - d1.getTimeInMillis())/1000;
		}
		return min;
	}
	
	/**
	 * 获取一段时间(秒)之后的具体时间
	 * @param duration
	 * @return
	 */
	public static Date getDateAfterSecond(long duration) {
		long curr = System.currentTimeMillis();
		curr = curr + duration * 1000;
		return new Date(curr);
	}
}
